package com.hjs.security;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class FileDecr  extends HttpServlet {
    public FileDecr() {
        super();
    }
    public void destroy() {
        super.destroy();
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String file=request.getParameter("file");
        String newFile=request.getParameter("newFile");
        String key=request.getParameter("key");
        boolean flag=DesFile.decryptFile(file,newFile,key);
        if (flag){
            request.setAttribute("msgDec","解密成功");
            String fileContent=DesFile.readFileByLine(newFile);
            if(fileContent!=null){
                request.setAttribute("fileContent",fileContent);
            }else {
                request.setAttribute("fileContent", "密钥不正确");
            }
            request.getRequestDispatcher("/file.jsp").forward(request, response);
        }else {
            request.setAttribute("msgDec","解密失败");
            request.getRequestDispatcher("/file.jsp").forward(request, response);
        }
    }
    public void init() throws ServletException {
    }
}
